//
//  TTRecyclingCommonViewCell.m
//  TaoTuo
//
//  Created by apple on 2020/9/18.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTRecyclingCommonViewCell.h"

@interface TTRecyclingCommonViewCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITextField *contentTF;
@property (nonatomic, strong) UILabel *rightLabel;
@property (nonatomic, strong) UIImageView *rightImgView;

@end

@implementation TTRecyclingCommonViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self setupSubviews];
        @weakify(self)
        [self.contentTF.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
            @strongify(self)
            self.model.content = x;
        }];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setModel:(TTRecyclingCellModel *)model
{
    _model = model;
    
    self.titleLabel.text = model.title;
    self.contentTF.placeholder = model.placeholder;
    self.contentTF.text = model.content;
    self.contentTF.keyboardType = model.keyboardType;
    self.rightLabel.text = model.rightString;
    
    if (model.cellType == arrowTpey) {
        
        self.rightLabel.hidden = YES;
        self.rightImgView.hidden = NO;
        self.contentTF.enabled = NO;
        
    } else if (model.cellType == labelType) {
        
        self.rightLabel.hidden = NO;
        self.rightImgView.hidden = YES;
        self.contentTF.enabled = YES;
        
    } else {
        
        self.rightLabel.hidden = YES;
        self.rightImgView.hidden = YES;
        self.contentTF.enabled = NO;
    }
    
}

- (void)setupSubviews
{
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@15);
        make.centerY.equalTo(self);
    }];
    
    [self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_right).mas_offset(-10);
        make.width.mas_equalTo(30);
        make.centerY.equalTo(self);
    }];
    
    [self.rightImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.centerY.equalTo(self.rightLabel);
    }];
    
    [self.contentTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_right).mas_offset(15);
        make.right.equalTo(self.rightLabel.mas_left).mas_offset(-5);
        make.centerY.equalTo(self);
    }];
    
}


- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) font:[UIFont systemFontOfSize:14 weight:UIFontWeightMedium]];
        [self.contentView addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UITextField *)contentTF
{
    if (!_contentTF) {
        _contentTF = [[UITextField alloc] init];
        _contentTF.font = [UIFont systemFontOfSize:14];
        _contentTF.textColor = UIColorHex(333333);
        _contentTF.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:_contentTF];
    }
    return _contentTF;
}

- (UILabel *)rightLabel
{
    if (!_rightLabel) {
        _rightLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:14];
        _rightLabel.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:_rightLabel];
    }
    return _rightLabel;
}

- (UIImageView *)rightImgView
{
    if (!_rightImgView) {
        _rightImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellarrow_4"]];
        [self.contentView addSubview:_rightImgView];
    }
    return _rightImgView;
}


@end
